package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.warehouse.manage.dao.FileMapper;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.entity.po.File;
import com.zhijian.warehouse.manage.entity.vo.request.FileSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.FileSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.FileVo;
import com.zhijian.warehouse.manage.holder.CurrentWebUserHolder;
import com.zhijian.warehouse.manage.service.FileService;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文件表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-03-18 20:57:59
 */
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper mapper;

    @Override
    public File selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public FileVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(File file) {
        Date now = new Date();
        file.setCreateTime(now);
        file.setUpdateTime(now);
        return mapper.insert(file) > 0 ? file.getId() : 0L;
    }

    @Override
    public boolean update(File file) {
        Date now = new Date();
        file.setUpdateTime(now);
        return mapper.updateByPrimaryKey(file) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(File record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(FileSaveOrUpdateVo saveOrUpdateVo) {
        File file = new File();
        BeanUtils.copyProperties(saveOrUpdateVo, file);
        return this.insert(file) != 0;
    }

    @Override
    public boolean modify(FileSaveOrUpdateVo saveOrUpdateVo) {
        File file = new File();
        BeanUtils.copyProperties(saveOrUpdateVo, file);
        return this.update(file);
    }

    @Override
    public FileVo selectOne(FileSearchVo fileSearchVo) {
        return mapper.selectOne(fileSearchVo);
    }

    @Override
    public Integer count(FileSearchVo fileSearchVo) {
        return mapper.count(fileSearchVo);
    }

    @Override
    public List<FileVo> selectList(FileSearchVo fileSearchVo) {
        return mapper.selectList(fileSearchVo);
    }

    @Override
    public Page<FileVo> selectPage(FileSearchVo fileSearchVo) {
        AssertCommonUtil.check(Objects.isNull(fileSearchVo.getPageNum()) || Objects.isNull(fileSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(fileSearchVo);
        //分页
        Page<FileVo> page = new Page<>(true, count, fileSearchVo.getPageNum(), fileSearchVo.getPageSize());
        fileSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<FileVo> responseVos = this.selectList(fileSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<File> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(FileSearchVo fileSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(fileSearchVo);
    }

    @Override
    public boolean insertBatch(List<FileSaveOrUpdateVo> saveOrUpdateVoList, Byte type, Long dataId) {
        //默认PC
        return insertBatch(saveOrUpdateVoList, type, dataId, CurrentWebUserHolder.getUser());
    }

    @Override
    public boolean insertBatch(List<FileSaveOrUpdateVo> saveOrUpdateVoList, Byte type, Long dataId, UserDto userDto) {
        if (CollectionUtil.isNotEmpty(saveOrUpdateVoList)) {
            Date now = new Date();
            return this.insertBatch(saveOrUpdateVoList.stream().map(g -> {
                File file = new File();
                file.setFilePath(g.getFilePath());
                file.setFileName(g.getFileName());
                file.setDataId(dataId);
                file.setType(type);
                file.setCreateUserId(userDto.getId());
                file.setCreateUserName(userDto.getName());
                file.setCreateTime(now);
                file.setUpdateTime(now);
                return file;
            }).collect(Collectors.toList()));
        }
        return false;
    }

}